<?php

class SiteController extends Controller
{
    #testowa wiamosc przekazana do widoku
    public $wlasnosc='Test222';
    public $wlasnosc_dwa='Test333';
    public $wlasnosc_trzy='';
    
	/**
	 * Declares class-based actions.
	 * @var int fdsdfs
         * @return boll fsdds
	 */
	public function actions()
	{
        
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
         * Defaultowa odpalana akcja przy uruchomieniu modullu
         * 
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
          $test_klasy_zewnetrznej = new CDamianTestAplikacja(); 
          
          $this->wlasnosc_trzy= $test_klasy_zewnetrznej->cos();
          
 //2011-11-07 wykorzystanie klas zewnetrznych poza polikacja zdef w freamworku
 //musze zaincludowac te klasy       
Yii::import('system.damian.*');
$test_klasy_zewnetrznej_freamwork = new CDamianTest();          
$this->wlasnosc_trzy= $test_klasy_zewnetrznej_freamwork->cos();

CVarDumper::dodaj($this->wlasnosc_trzy,'wiadomosc pochodzaca z klasy z poza aplikacji');
            #tes CLIST
            $cas[] = 'a';
            $cas[] = 'b';
            $cas[] = 'c';
            $cas[] = 'd';
            
            $cas[0][0] = 'aa';
            $cas[1][0] = 'bb';
            $cas[2][0] = 'cc';
            
            $lista = new CList($cas);
            $lista[]='e';
            $ile=$lista->count();
            $this->wlasnosc_dwa=$lista;
            
            /*
            $list[]=$item;  // append at the end
$list[$index]=$item; // $index must be between 0 and $list->Count
unset($list[$index]); // remove the item at $index
if(isset($list[$index])) // if the list has an item at $index
foreach($list as $index=>$item) // traverse each item in the list
$n=count($list); // returns the number of items in the list
*/
            
            $this->wlasnosc=$ile;
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
            
            
            
            $apps1 = array(
                            'apps' => array(
                                            'task tracking',
                                            'bug tracking',
                                             ),
                            'is_new' => false
                           );
            
        $apps2 = array(
                        'apps' => array(
                                        'blog',
                                        'task tracking',
                                        ),
                        'todo' => array(
                                        'buy milk',
                                        ),
                        'is_new' => true
                        );
        $apps = CMap::mergeArray($apps1, $apps2);
        CVarDumper::dodaj($apps,'pierwszy- dev');
        
        $cas[] = 'a';
        $cas[] = 'b';
        $cas[] = 'c';

        $cas[0][0] = 'aa';
        $cas[1][0] = 'bb';
        $cas[2][0] = 'cc';
        CVarDumper::dodaj($cas, 'drugi- dev');
        
       
            
            //przekazanie do widoku zmiennej
            $argument_render['nazwa_zmiennej']=$this->wlasnosc;
            $this->render('index', $argument_render);
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
        
 #################################MOJE AKCJE TESTOWE#########################
        /**
         * Test kolejek
	 * 2011-11-08
	 */
	public function actionTestKolejek()
	{
            #1. KOLEJKA  do której dostęp jest w stylu obiektowym
		$col = new CAttributeCollection();
                // $col->add('name','Alexander');
                $col->name='Alexander';
                // echo $col->itemAt('name');
                $this->wlasnosc= $col->name;
                $this->render('testKolejek');
                
                
                
             #2. Stos LIFO
            $garage = new CStack();
            // getting some cars into the garage
            $garage->push(new TestCar('Ferrari'));
            $garage->push(new TestCar('Porsche'));
            $garage->push(new TestCar('Kamaz'));
            
            #pobranie bez usunięcia z kolejki  
            $peek=$garage->peek(); // Kamaz!
            echo '<pre>';
            print_r($peek);
            echo '</pre>';
          
            #pobranie z usunięciem z kolejki
            $garage->pop(); // usuniecie ze stosu Kamaza
            $porsche = $garage->pop();// pobranie porshee i usuniecie
            $porsche->jedz();

                
	}
        
}